package com.fr.data.core;

import com.fr.base.ArrayUtils;
import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.dav.Env;
import com.fr.base.file.DatasourceManager;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.db.TableProcedure;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dml.Select;
import com.fr.data.core.db.dml.Table;
import com.fr.data.core.db.handler.SQLTypeHandler;
import com.fr.data.core.db.handler.SQLTypeHandlerFactory;
import com.fr.data.impl.Connection;
import com.fr.data.impl.DBTableData;
import com.fr.data.impl.DatabaseConnection;
import com.fr.data.util.function.DataFunction;
import com.fr.report.TableDataSource;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.web.ui.CustomWidgetConfig;
import com.fr.report.web.ui.WidgetConfig;
import com.fr.report.web.ui.WidgetManager;
import com.fr.util.Utils;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.OracleTypes;

public class DataUtils
{
  private static Map databaseSQLTablesMap = new HashMap();
  private static Map schemaMap = new HashMap();
  private static Map tableMap = new HashMap();
  private static Map viewMap = new HashMap();
  private static Map procedureMap = new HashMap();

  public static int calculateOracleType(String paramString)
  {
    Field localField;
    try
    {
      localField = OracleTypes.class.getDeclaredField(paramString.toUpperCase());
      return localField.getInt(OracleTypes.class);
    }
    catch (Exception localException)
    {
    }
    return -10;
  }

  public static String getFunctionDisplayName(DataFunction paramDataFunction)
  {
    if (paramDataFunction == null)
      return "";
    String str = paramDataFunction.getClass().getName();
    int i = str.lastIndexOf(".");
    if (i != -1)
      ++i;
    else
      i = 0;
    StringBuffer localStringBuffer = new StringBuffer("DataFunction-");
    localStringBuffer.append(str.substring(i, str.length() - "Function".length()));
    return Inter.getLocText(localStringBuffer.toString());
  }

  public static String createSelectSQL(String paramString1, String paramString2)
  {
    return createSelectSQL(new Table(paramString1, paramString2), null, null);
  }

  public static String createSelectSQL(Table paramTable)
  {
    return createSelectSQL(paramTable, null, null);
  }

  public static String createSelectSQL(Table paramTable, String[] paramArrayOfString, Dialect paramDialect)
  {
    Select localSelect = new Select(paramTable);
    int i = 0;
    int j = (paramArrayOfString == null) ? 0 : paramArrayOfString.length;
    while (i < j)
    {
      localSelect.addColumnName(paramArrayOfString[i]);
      ++i;
    }
    return localSelect.toNormalSQLString(paramDialect);
  }

  // ERROR //
  public static Object getResultSetObject(ResultSet paramResultSet, int paramInt1, int paramInt2, String paramString1, String paramString2, Dialect paramDialect)
    throws SQLException
  {
    // Byte code:
    //   0: aload_0
    //   1: iload_1
    //   2: iload_2
    //   3: aload 5
    //   5: invokestatic 35	com/fr/data/core/DataUtils:getResultSetObject	(Ljava/sql/ResultSet;IILcom/fr/data/core/db/dialect/Dialect;)Ljava/lang/Object;
    //   8: astore 6
    //   10: aload_3
    //   11: ifnull +85 -> 96
    //   14: aload_3
    //   15: invokevirtual 22	java/lang/String:length	()I
    //   18: ifle +78 -> 96
    //   21: aload 6
    //   23: instanceof 36
    //   26: ifeq +70 -> 96
    //   29: aload 4
    //   31: ifnull +30 -> 61
    //   34: aload 4
    //   36: invokevirtual 22	java/lang/String:length	()I
    //   39: ifle +22 -> 61
    //   42: new 36	java/lang/String
    //   45: dup
    //   46: aload 6
    //   48: checkcast 36	java/lang/String
    //   51: aload_3
    //   52: invokevirtual 37	java/lang/String:getBytes	(Ljava/lang/String;)[B
    //   55: aload 4
    //   57: invokespecial 38	java/lang/String:<init>	([BLjava/lang/String;)V
    //   60: areturn
    //   61: new 36	java/lang/String
    //   64: dup
    //   65: aload 6
    //   67: checkcast 36	java/lang/String
    //   70: aload_3
    //   71: invokevirtual 37	java/lang/String:getBytes	(Ljava/lang/String;)[B
    //   74: invokespecial 39	java/lang/String:<init>	([B)V
    //   77: areturn
    //   78: astore 7
    //   80: invokestatic 41	com/fr/base/FRContext:getLogger	()Ljava/util/logging/Logger;
    //   83: getstatic 42	java/util/logging/Level:WARNING	Ljava/util/logging/Level;
    //   86: aload 7
    //   88: invokevirtual 43	java/io/UnsupportedEncodingException:getMessage	()Ljava/lang/String;
    //   91: aload 7
    //   93: invokevirtual 44	java/util/logging/Logger:log	(Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
    //   96: aload 6
    //   98: ifnonnull +9 -> 107
    //   101: getstatic 45	com/fr/report/script/Primitive:NULL	Lcom/fr/report/script/Primitive;
    //   104: goto +5 -> 109
    //   107: aload 6
    //   109: areturn
    //
    // Exception table:
    //   from	to	target	type
    //   29	60	78	java/io/UnsupportedEncodingException
    //   61	77	78	java/io/UnsupportedEncodingException
  }

  public static WidgetConfig getClonedWidgetConfig(WidgetManager paramWidgetManager, String paramString)
    throws Exception
  {
    WidgetConfig localWidgetConfig = paramWidgetManager.getWidgetConfig(paramString);
    if (localWidgetConfig instanceof CustomWidgetConfig)
      ((CustomWidgetConfig)localWidgetConfig).setWidgetType(paramString);
    if (localWidgetConfig != null)
      return ((WidgetConfig)localWidgetConfig.clone());
    if (localWidgetConfig == null)
      throw new Exception("No such widget with name \"" + paramString + "\".");
    return localWidgetConfig;
  }

  public static TableData getClonedTableData(DatasourceManager paramDatasourceManager, String paramString)
    throws Exception
  {
    TableData localTableData = paramDatasourceManager.getTableData(paramString);
    if (localTableData != null)
      return localTableData;
    String str1 = null;
    String str2 = null;
    int i = -1;
    if (paramString != null)
      i = paramString.indexOf(":");
    if (i != -1)
    {
      str1 = paramString.substring(0, i);
      str2 = paramString.substring(i + 1);
    }
    if ((str1 != null) && (str2 != null))
    {
      String str3;
      DatabaseConnection localDatabaseConnection;
      Iterator localIterator = paramDatasourceManager.getConnectionNameIterator();
      do
      {
        Connection localConnection;
        do
        {
          if (!(localIterator.hasNext()))
            break label195;
          str3 = (String)localIterator.next();
          localConnection = paramDatasourceManager.getConnection(str3);
        }
        while (!(localConnection instanceof DatabaseConnection));
        localDatabaseConnection = (DatabaseConnection)localConnection;
      }
      while (!(str3.equals(str1)));
      TableProcedure[] arrayOfTableProcedure = getDatabaseSQLTables(localDatabaseConnection, null);
      int j = 0;
      while (true)
      {
        if (j >= arrayOfTableProcedure.length);
        if (ComparatorUtils.equals(str2, arrayOfTableProcedure[j].getName()))
          return new DBTableData(localDatabaseConnection, createSelectSQL(arrayOfTableProcedure[j].getSchema(), arrayOfTableProcedure[j].getName()));
        ++j;
      }
    }
    if (localTableData == null)
      label195: throw new Exception("No such tabledata with name \"" + paramString + "\".");
    return localTableData;
  }

  public static Object getResultSetObject(ResultSet paramResultSet, int paramInt1, int paramInt2, Dialect paramDialect)
    throws SQLException
  {
    SQLTypeHandlerFactory localSQLTypeHandlerFactory = SQLTypeHandlerFactory.buildSQLTypeHanderFactory();
    SQLTypeHandler localSQLTypeHandler = localSQLTypeHandlerFactory.getTypeHandler(new Integer(paramInt1));
    return localSQLTypeHandler.getValue(paramResultSet, paramInt2, paramInt1, paramDialect);
  }

  public static String writeDictObject(Object paramObject)
  {
    if (paramObject == null)
      return "null:null";
    if (paramObject instanceof String)
      return ((String)paramObject);
    if (paramObject instanceof Number)
    {
      if (paramObject instanceof Integer)
        return paramObject + ":I";
      if (paramObject instanceof Long)
        return paramObject + ":L";
      if (paramObject instanceof Float)
        return paramObject + ":F";
      if (paramObject instanceof Double)
        return paramObject + ":D";
      if (paramObject instanceof BigInteger)
        return paramObject + ":BI";
      if (!(paramObject instanceof BigDecimal))
        break label251;
      return paramObject + ":BD";
    }
    if (paramObject instanceof Boolean)
      return paramObject + ":B";
    if (paramObject instanceof Date)
      return DateUtils.DATEFORMAT2.format((Date)paramObject) + ":DT";
    label251: return paramObject.toString();
  }

  public static Object readDictObject(String paramString)
  {
    Object localObject = paramString;
    if ((paramString != null) && (paramString.matches(".*:.*")))
    {
      if (paramString.equals("null:null"))
        return null;
      try
      {
        if (paramString.endsWith(":I"))
          localObject = Integer.valueOf(paramString.substring(0, paramString.length() - 2));
        else if (paramString.endsWith(":L"))
          localObject = Long.valueOf(paramString.substring(0, paramString.length() - 2));
        else if (paramString.endsWith(":F"))
          localObject = Float.valueOf(paramString.substring(0, paramString.length() - 2));
        else if (paramString.endsWith(":D"))
          localObject = Double.valueOf(paramString.substring(0, paramString.length() - 2));
        else if (paramString.endsWith(":BI"))
          localObject = new BigInteger(paramString.substring(0, paramString.length() - 3));
        else if (paramString.endsWith(":BD"))
          localObject = new BigDecimal(paramString.substring(0, paramString.length() - 3));
      }
      catch (NumberFormatException localNumberFormatException)
      {
        localObject = paramString;
      }
      if (paramString.endsWith(":B"))
        localObject = Boolean.valueOf(paramString.substring(0, paramString.length() - 2));
      else if (paramString.endsWith(":DT"))
        localObject = DateUtils.object2Date(paramString.substring(0, paramString.length() - 3), true);
    }
    return localObject;
  }

  public static Object getTableDataValue(TableData paramTableData, int paramInt1, int paramInt2)
  {
    if (paramInt2 < -1)
      return null;
    try
    {
      return ((paramInt2 == -1) ? new Integer(paramInt1 + 1) : paramTableData.getValueAt(paramInt1, paramInt2));
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    return null;
  }

  public static String getOnlyColumnName(List paramList, String paramString)
  {
    int i;
    paramString = Utils.replaceAllString(paramString, " ", "_");
    if (paramList.contains(paramString))
      for (i = 2; i < 1200; ++i)
      {
        String str = paramString + "_" + i;
        if (!(paramList.contains(str)))
          return str;
      }
    return paramString;
  }

  public static TableProcedure[] getDatabaseSQLTables(DatabaseConnection paramDatabaseConnection, String paramString)
    throws Exception
  {
    Map localMap = (Map)databaseSQLTablesMap.get(paramDatabaseConnection);
    TableProcedure[] arrayOfTableProcedure = null;
    if (localMap == null)
    {
      HashMap localHashMap = new HashMap();
      arrayOfTableProcedure = FRContext.getCurrentEnv().getSQLTablesProcedure(paramDatabaseConnection, paramString);
      if (arrayOfTableProcedure == null)
        arrayOfTableProcedure = new TableProcedure[0];
      localHashMap.put(paramString, arrayOfTableProcedure);
      databaseSQLTablesMap.put(paramDatabaseConnection, localHashMap);
    }
    else
    {
      arrayOfTableProcedure = (TableProcedure[])localMap.get(paramString);
      if (arrayOfTableProcedure == null)
      {
        arrayOfTableProcedure = FRContext.getCurrentEnv().getSQLTablesProcedure(paramDatabaseConnection, paramString);
        if (arrayOfTableProcedure == null)
          arrayOfTableProcedure = new TableProcedure[0];
        localMap.put(paramString, arrayOfTableProcedure);
        databaseSQLTablesMap.put(paramDatabaseConnection, localMap);
      }
    }
    return arrayOfTableProcedure;
  }

  public static void refreshTables(DatabaseConnection paramDatabaseConnection, String paramString)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    TableProcedure[] arrayOfTableProcedure = FRContext.getCurrentEnv().getSQLTablesProcedure(paramDatabaseConnection, paramString);
    if (arrayOfTableProcedure == null)
      arrayOfTableProcedure = new TableProcedure[0];
    localHashMap.put(paramString, arrayOfTableProcedure);
    databaseSQLTablesMap.put(paramDatabaseConnection, localHashMap);
  }

  public static String[] getDatabaseSchema(DatabaseConnection paramDatabaseConnection)
  {
    String[] arrayOfString = (String[])schemaMap.get(paramDatabaseConnection);
    if (arrayOfString == null)
      try
      {
        arrayOfString = FRContext.getCurrentEnv().getTableSchema(paramDatabaseConnection);
        if (arrayOfString == null)
          arrayOfString = new String[0];
        schemaMap.put(paramDatabaseConnection, arrayOfString);
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    return arrayOfString;
  }

  public static void refreshSchema(DatabaseConnection paramDatabaseConnection)
  {
    String[] arrayOfString;
    try
    {
      arrayOfString = FRContext.getCurrentEnv().getTableSchema(paramDatabaseConnection);
      if (arrayOfString == null)
        arrayOfString = new String[0];
      schemaMap.put(paramDatabaseConnection, arrayOfString);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
  }

  public static TableProcedure[] getTables(DatabaseConnection paramDatabaseConnection, String paramString1, String paramString2, boolean paramBoolean)
    throws Exception
  {
    Map localMap1;
    if (paramString1.equals("TABLE"))
      localMap1 = tableMap;
    else if (paramString1.equals("VIEW"))
      localMap1 = viewMap;
    else if (paramString1.equals("PROCEDURE"))
      localMap1 = procedureMap;
    else
      return null;
    if ((paramBoolean) && (paramString1.equals("TABLE")))
      paramString1 = "ALLTABLE";
    Map localMap2 = (Map)localMap1.get(paramDatabaseConnection);
    TableProcedure[] arrayOfTableProcedure = null;
    if (localMap2 == null)
    {
      HashMap localHashMap = new HashMap();
      arrayOfTableProcedure = FRContext.getCurrentEnv().getTableProcedure(paramDatabaseConnection, paramString1, paramString2);
      if (arrayOfTableProcedure == null)
        arrayOfTableProcedure = new TableProcedure[0];
      localHashMap.put(paramString2, arrayOfTableProcedure);
      localMap1.put(paramDatabaseConnection, localHashMap);
    }
    else
    {
      arrayOfTableProcedure = (TableProcedure[])localMap2.get(paramString2);
      if (arrayOfTableProcedure == null)
      {
        arrayOfTableProcedure = FRContext.getCurrentEnv().getTableProcedure(paramDatabaseConnection, paramString1, paramString2);
        if (arrayOfTableProcedure == null)
          arrayOfTableProcedure = new TableProcedure[0];
        localMap2.put(paramString2, arrayOfTableProcedure);
        localMap1.put(paramDatabaseConnection, localMap2);
      }
    }
    return arrayOfTableProcedure;
  }

  public static void refreshTables(DatabaseConnection paramDatabaseConnection, String paramString1, String paramString2)
  {
    Map localMap1;
    if (paramString1.equals("TABLE"))
      localMap1 = tableMap;
    else if (paramString1.equals("VIEW"))
      localMap1 = viewMap;
    else if (paramString1.equals("PROCEDURE"))
      localMap1 = procedureMap;
    else
      return;
    Map localMap2 = (Map)localMap1.get(paramDatabaseConnection);
    if (localMap2 == null)
      return;
    localMap2.remove(paramString2);
  }

  public static TableData getTableData4Function(Calculator paramCalculator, String paramString)
  {
    TableDataSource localTableDataSource = paramCalculator.getCurrentTableDataSource();
    if (localTableDataSource == null)
      return null;
    TableData localTableData = null;
    localTableData = localTableDataSource.getTableData(paramString);
    if (localTableData != null)
      setTableDataParameterValue(paramCalculator, localTableData.getParameters());
    if (localTableData == null)
    {
      DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
      try
      {
        localTableData = getClonedTableData(localDatasourceManager, paramString);
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
      localTableDataSource.putTableData(paramString, localTableData);
      setTableDataParameterValue(paramCalculator, localTableData.getParameters());
    }
    return localTableData;
  }

  private static void setTableDataParameterValue(Calculator paramCalculator, Parameter[] paramArrayOfParameter)
  {
    int i;
    if (ArrayUtils.isEmpty(paramArrayOfParameter))
      return;
    if (!(ArrayUtils.isEmpty(paramArrayOfParameter)))
      for (i = 0; i < paramArrayOfParameter.length; ++i)
      {
        Parameter localParameter = paramArrayOfParameter[i];
        Object localObject = paramCalculator.resolveVariable(localParameter.getName());
        if (localObject != null)
          localParameter.setValue(localObject);
      }
  }
}